using System;
using System.Collections.Generic;
using UnityEngine;
using QFramework;
using Quaternion = UnityEngine.Quaternion;
using Vector3 = UnityEngine.Vector3;

namespace Survivor
{
	public partial class SimpleCircle : ViewController
	{
		public List<Circle> Circles { get; set; } = new List<Circle>();
		void Start()
		{
			UpdateCircles();
			Global.SimpleCircleCount.Register(UpdateCircles).UnRegisterWhenGameObjectDestroyed(this);
			Global.FlyThingCount.Register(UpdateCircles).UnRegisterWhenGameObjectDestroyed(this);
		}
		

		/// <summary>
		/// 更新环绕物数量
		/// </summary>
		/// <param name="value">这里的value没有意义,主要是为了能够注册的取消方法</param>
		public void UpdateCircles(int value = 0)
		{
			if (!this.isActiveAndEnabled) return;
			for (int i = Circles.Count; i < Global.SimpleCircleCount.Value + Global.FlyThingCount.Value; i++)
			{
				Circle.Instantiate().Show().Self((self) =>
				{
					var cpt = self.GetComponent<Circle>();
					Circles.Add(cpt);
				});
			}
			for (int i = 0; i < Circles.Count; i++)
			{
				var cpt = Circles[i];
				cpt.Init(this.transform, 360f / Circles.Count * i);
			}
		}

		private void OnDestroy()
		{
			foreach (var circle in Circles)
			{
				Destroy(circle);
			}
			Circles.Clear();
		}
	}
}
